package com.simpleIniReader 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Omar Contreras
	 */
	public class simpleIniReader extends EventDispatcher
	{
		var fileLoader:URLLoader;
		var _arguments:Dictionary;
		var lines:Array;
		
		public function simpleIniReader(fileName:String = null) 
		{
			if(fileName)
				fileLoader = new URLLoader(new URLRequest(fileName));
			else 
				return;
			
			_arguments = new Dictionary();
			lines = new Array();
			
			fileLoader.addEventListener(Event.COMPLETE, onFileLoaded);
		}
		
		private function onFileLoaded(e:Event):void 
		{
			var data:String = String(e.target.data);
			lines = data.split("\r\n");
			
			for each(var line:String in lines)
			{
				var splitLine:Array = line.split('=');
				_arguments[splitLine[0]] = splitLine[1];
			}
			
			dispatchEvent(new simpleIniFileReaderEvent(simpleIniFileReaderEvent.CONFIG_LOADED));
		}
		
		public function getArgumentByName(name:String = null):String
		{
			if (_arguments[name])
			{
				return _arguments[name];
			}
			
			return null;
		}
		
	}

}